﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 小小次朗点餐系统
{
    public partial class Enroll : Form
    {
        public Enroll()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            if (Accounts.logon == "1")
            {
                //设定画面名称
                this.Text = "重置密码";
                //设定按钮显示
                this.enroll_button.Text = "重置密码";
                this.enroll_genre_emptor.Text = "用户";
                
                    this.enroll_genre_seller.Text = "商家";
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {


            //验证输入框是否为空
            if (string.IsNullOrEmpty(enroll_phone.Text) ||
                string.IsNullOrEmpty(enroll_password.Text) ||
                string.IsNullOrEmpty(enroll_password.Text)
                )
            {
                MessageBox.Show("账号密码不能为空");
                return;
            }
            //验证密码与确认密码是否相同
            if (enroll_password.Text != enroll_password.Text)
            {
                MessageBox.Show("两次密码不一致");
                return;
            }
            //验证验证码是否正确
            if (enroll_proving.Text != "123456")
            {
                MessageBox.Show("验证码有误");
                return;
            }


            if (Accounts.logon == "0")
            {
                //判断用户类型，true为用户,并尝试注册
                if (enroll_genre_emptor.Checked == true)
                {
                    if (enroll_phone.Text != "")
                    {    //判断该账号是否已存在于数据库
                        string sql = string.Format($"select * from [User] where Unumber='{enroll_phone.Text}'");

                        DataSet ds = DBHelper.SelectByAdapter(sql);

                        DataTable dt = ds.Tables[0];

                        //数据库返回行数，说明该帐号存在

                        if (dt.Rows.Count > 0)
                        {

                            MessageBox.Show("该账号已存在");
                            return;
                        }
                        else
                        {
                            string sql1 = string.Format($"insert into [User] (Unumber,pwd) values('{enroll_phone.Text}','{enroll_password.Text}')");
                            bool rlt = DBHelper.GetNonQuery(sql1);
                            if (rlt)
                            {
                                MessageBox.Show("注册成功");
                                Close();
                            }
                            else
                            {
                                MessageBox.Show("注册失败");
                                return;
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("不能为空");
                    }


                }
                else//flase为商家，尝试注册
                {
                    if (enroll_phone.Text != "")
                    {
                        string sql = string.Format($"select * from Seller where Senumber='{enroll_phone.Text}'");

                        DataSet ds = DBHelper.SelectByAdapter(sql);

                        DataTable dt = ds.Tables[0];

                        if (dt.Rows.Count > 0)
                        {

                            MessageBox.Show("该账号已存在");
                            return;
                        }
                        else
                        {
                            string sql1 = string.Format($"insert into Seller (Senumber,pwd) values('{enroll_phone.Text}','{enroll_password.Text}')");
                            bool rlt = DBHelper.GetNonQuery(sql1);
                            if (rlt)
                            {
                                MessageBox.Show("注册成功");
                                Close();
                            }
                            else
                            {
                                MessageBox.Show("注册失败");
                                return;
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("不能为空");
                    }
                }
            }


            //******************************重置密码
            if (Accounts.logon == "1")
            {
                if (enroll_genre_emptor.Checked == true)//true为用户 重置密码
                {
                    if (enroll_phone.Text != "")
                    {    //判断该账号是否已存在于数据库
                        string sql = string.Format($"select * from [User] where Unumber='{enroll_phone.Text}'");

                        DataSet ds = DBHelper.SelectByAdapter(sql);

                        DataTable dt = ds.Tables[0];

                        //数据库返回行数，说明该帐号存在

                        if (dt.Rows.Count > 0)
                        {
                            string sql1 = string.Format($"update [User] set Unumber='{enroll_phone.Text}',pwd='{enroll_password.Text}'");
                            bool rlt = DBHelper.GetNonQuery(sql1);
                            if (rlt)
                            {
                                MessageBox.Show("修改成功");
                                Close();
                            }
                            else
                            {
                                MessageBox.Show("修改失败");
                                return;
                            }
                        }
                        else
                        {
                            MessageBox.Show("该账号未注册");
                            return;
                        }


                    }
                    else//flase为商家，重置密码
                    {
                        if (enroll_phone.Text != "")
                        {   //判断该账号是否已存在于数据库
                            string sql = string.Format($"select * from Seller where Senumber='{enroll_phone.Text}'");

                            DataSet ds = DBHelper.SelectByAdapter(sql);

                            DataTable dt = ds.Tables[0];
                            //数据库返回行数，说明该帐号存在
                            if (dt.Rows.Count > 0)
                            {
                                string sql1 = string.Format($"update Food1 Seller Senumber='{enroll_phone.Text}',pwd='{enroll_password.Text}'");
                                bool rlt = DBHelper.GetNonQuery(sql);
                                if (rlt)
                                {
                                    MessageBox.Show("修改成功");
                                    Close();
                                }
                                else
                                {
                                    MessageBox.Show("修改失败");
                                    return;
                                }
                            }
                            else
                            {
                                MessageBox.Show("该账号未注册");
                                return;
                            }
                        }
                    }
                }
            }
        }

        private void Form2_Load_1(object sender, EventArgs e)
        {
           
        }
    }
}
